package com.iscas.dataaggregation.controller;


import com.iscas.dataaggregation.common.CustomResult;
import com.iscas.dataaggregation.domain.experiment.OtelLogs;
import com.iscas.dataaggregation.service.OtelLogsService;
import com.iscas.dataaggregation.utils.ChaosDataProcessor;
import com.iscas.dataaggregation.utils.ChaosFaultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/experimentdata")
public class ExperimentController {
    @Autowired
    private OtelLogsService otelLogsService;

    @Autowired
    private ChaosDataProcessor chaosDataProcessor;
    /**
     * 根据时间戳区间查询日志
     *
     * @param startTimestamp 开始时间戳（毫秒）
     * @param endTimestamp   结束时间戳（毫秒）
     * @return 查询到的日志列表
     */
    @GetMapping("/logs")
    public ResponseEntity<List<OtelLogs>> getLogsByTimestamp(
            @RequestParam("startTimestamp") Long startTimestamp,
            @RequestParam("endTimestamp") Long endTimestamp) {
        // 转换 Long 类型时间戳为 Date 类型
        Date startTime = new Date(startTimestamp);
        Date endTime = new Date(endTimestamp);

        // 查询日志
        List<OtelLogs> logs = otelLogsService.findLogsByTimeRange(startTime, endTime);

        // 返回结果
        return ResponseEntity.ok(logs);
    }


    @GetMapping("/getData")
    public CustomResult getExperimentData(@RequestParam("planId") int planId, @RequestParam("namespace") String namespace){
        String result = chaosDataProcessor.processPlan(planId, namespace);
        return CustomResult.ok(result);
    }
}
